using UnityEngine;
using QFramework;
using QAssetBundle;

namespace ProjectSurvivor
{
    public partial class Player : ViewController
    {
        public float MovementSpeed = 5;
        public static Player Default;
        private AudioPlayer mWalkSfx;
        private void Awake()
        {
            Default = this;
        }

        private void OnDestroy()
        {
            Default = null;
        }
        void Start()
        {
            // Code Here
            "Hello! QFramework!".LogInfo();

            HurtBox.OnTriggerEnter2DEvent((collider2D) =>
            {
                var hitBox = collider2D.GetComponent<HurtAndHitBox>();

                if (hitBox)
                {
                    if (hitBox.Owner.CompareTag("Enemy"))
                    {
                        Global.Hp.Value--;
                        if (Global.Hp.Value <= 0)
                        {
                            AudioKit.PlaySound("Die");
                            this.DestroyGameObjGracefully();
                            ResKit.Init();
                            UIKit.OpenPanel<TestUIGameOverPanel>();
                        }
                        else
                        {
                            AudioKit.PlaySound("Hurt");
                        }
                    }
                }

            }).UnRegisterWhenGameObjectDestroyed(gameObject);

            void UpDateHp()
            {
                HpValue.fillAmount = Global.Hp.Value / (float)Global.MaxHp.Value;
            }
            Global.Hp.RegisterWithInitValue((hp)=>
            {
                UpDateHp();

            }).UnRegisterWhenGameObjectDestroyed(gameObject);

            Global.MaxHp.RegisterWithInitValue((maxhp) =>
            {
                UpDateHp();
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
        }
        private bool mFaceRight = true;
        private void Update()
        {
            var horizontal = Input.GetAxisRaw("Horizontal");//1
            var vertical = Input.GetAxisRaw("Vertical");//1
            var targetVelocity = new Vector2(horizontal, vertical).normalized * (MovementSpeed * Global.MovementSpeedRate.Value);
            if(horizontal ==0 && vertical == 0)
            {
                if(mFaceRight)
                {
                    Sprite.Play("Player_Ideal_Right");
                }
                else
                {
                    Sprite.Play("Player_Ideal_Left");
                }

                if(mWalkSfx!=null)
                {
                    mWalkSfx.Stop();
                    mWalkSfx = null;
                }
            }
            else
            {
                if(mWalkSfx==null)
                {
                    mWalkSfx = AudioKit.PlaySound(Sfx.WALK,true);
                }
                if(horizontal >0)
                {
                    mFaceRight = true;
                }
                else
                {
                    mFaceRight = false;
                }

                if (mFaceRight)
                {
                    Sprite.Play("Player_Walk_Right");
                }
                else
                {
                    Sprite.Play("Player_Walk_Left");
                }
            }


            SelfRigidbody2D.velocity = Vector2.Lerp(SelfRigidbody2D.velocity, targetVelocity, 1 - Mathf.Exp(-Time.deltaTime * 5));

        }
    }
}
